import java.util.Scanner;

public class NGuessNumber {
    public static void main(String[] args) {
        String playAgain;
        do {
            int answer[] = generateAnswer();
            System.out.println("游戏开始");
            boolean isWin = false;
            while (!isWin) {
                System.out.println("请输入数字，按回车分割");
                int[] playerInput = getPlayerInput();
                String result = compareGuessAnswer(playerInput, answer);
                System.out.println(result);
                isWin = isWin(result);
                System.out.println(isWin ? "恭喜你猜对了！" : "很遗憾，你猜错了！");
            }
            //在玩一局
            System.out.println("是否再玩一局？(y/n)");
            Scanner scanner = new Scanner(System.in);
            playAgain = scanner.next();
        } while (playAgain.equals("y"));
    }

    //随机生成4个0-9之间的不重复的整型数作为答案并返回
    public static int[] generateAnswer() {
        int[] answer = new int[4];
        for (int i = 0; i < answer.length; i++) {
            answer[i] = (int) (Math.random() * 10);
        }
        for (int i = 0; i < answer.length; i++) {
            for (int j = 0; j < answer.length; j++) {
                if (answer[i] == answer[j] && i != j) {
                    answer[i] = (int) (Math.random() * 10);
                    j = 0;
                }
            }
        }
        return answer;
    }

    //输入
    public static int[] getPlayerInput() {
        Scanner scanner = new Scanner(System.in);
        int[] nums = new int[4];
        for (int i = 0; i < 4; i++) {
            nums[i] = scanner.nextInt();
        }
        return nums;
    }

    public static String compareGuessAnswer(int[] guess, int[] answer) {
        int a_num = 0;
        int b_num = 0;
        for (int i = 0; i < answer.length; i++) {
            for (int l=0; l < answer.length; l++){
                if(i!=l){
                    if(answer[i]==guess[l]){
                        b_num++;
                    }
                }else{
                    if(answer[i]==guess[l]){
                        a_num++;
                    }
                }

            }
        }
        System.out.println(a_num+"A"+b_num+"B");
        System.out.println("A表示存在且位置正确的数字个数，B表示存在且位置不正确的数字个数！");
        System.out.println("如：1A2B,表示你猜测的四个数字有3个是对的，有一个位置对，两个位置不对。\n");
        return a_num+"A"+b_num+"B";
    }

    public static boolean isWin(String answer) {
        if (answer.charAt(0) == '4') {
            return true;
        } else {
            return false;
        }
    }
}
